﻿Imports System.Data.OleDb
Module FuncUtiles

    'Variables Globales en donde realiza la suma de los puntos de la auditoria

    'ATENCION
    Public Psaludo As Double = 0
    Public Pempatia As Double = 0
    Public Pindagacion As Double = 0
    Public Pdisposicion As Double = 0
    Public Pcompromiso As Double = 0
    Public Pinformacion As Double = 0
    Public Pcomunicacion As Double = 0
    Public Pcredibilidad As Double = 0
    Public Pcierre As Double = 0
    Public puntajeTotal As Double = 0
    '-----------------------------------
    'VENTAS
    Public PsaludoVTA As Double = 0
    Public PempatiaVTA As Double = 0
    Public PindagacionVTA As Double = 0
    Public PmanejoVTA As Double = 0
    Public PentusiasmoVTA As Double = 0
    Public PspeechVTA As Double = 0
    Public PpresentacionVTA As Double = 0
    Public PmecanismoVTA As Double = 0
    Public PpideVTA As Double = 0
    Public puntajeTotalVTA As Double = 0
    '-----------------------------------
    'POSVENTAS
    Public PSaludoPVTA As Double = 0
    Public PEmpatiaPVTA As Double = 0
    Public PEscuchaPVTA As Double = 0
    Public PIndagacionPVTA As Double = 0
    Public PDisposicionPVTA As Double = 0
    Public PPrecisionPVTA As Double = 0
    Public PComunicacionPVTA As Double = 0
    Public PCierrePVTA As Double = 0
    Public puntajeTotalPVTA As Double = 0




    '///////////////////////////////////////////////////////////////////////////////////////
    'Cadena de conexion y apertura de la base
    '-----------------------------------------
    Public Function cadenaConexBD() As OleDbConnection
        Dim conexion = New OleDbConnection()
        conexion.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\db\auditoria.mdb;"
        Try
            conexion.Open()
        Catch mierror As Exception
            MessageBox.Show("Error al conectar Base de Datos, no se pudo abrir la conexión" & mierror.Message)
        End Try
        Return (conexion)
    End Function

    '-----------------------------------------------------------------------------------------
    'FUNCION PARA COMPACTAR Y REPARAR LA BASE DE DATOS CADA VEZ QUE SE CIERRE EL SISTEMA
    '-----------------------------------------------------------------------------------------
    Sub CompactaryReparar()

        'nombre temporal para la base que se esta compactando
        Dim nameTempDB As String
        Dim pathBaseTemp As String
        Dim newBaseCompact As String
        Dim jro As JRO.JetEngine
        'nombre temporal para la base que se esta compactando
        nameTempDB = "DBT_" & Format$(Minute(Now), "00") & Format$(Second(Now), "00") & ".mdb"
        jro = New JRO.JetEngine()
        'Compacta la base y crea una temporal
        jro.CompactDatabase("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\db\auditoria.mdb", _
        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\db\" & nameTempDB & ";Jet OLEDB:Engine Type=5")
        ' Elimina la base original
        Kill(Application.StartupPath & "\db\auditoria.mdb")
        pathBaseTemp = Application.StartupPath & "\db\" & nameTempDB
        newBaseCompact = Application.StartupPath & "\db\auditoria.mdb"
        'Renombra la base temporal al nombre original de la base "auditoria.mdb"
        Rename(pathBaseTemp, newBaseCompact)
    End Sub

    '===================================================================
    'Funciones de control
    '===================================================================

    'Convierto en titulo los txt
    '--------------------------------
    Public Sub titulo(ByVal contenedor As Control)
        Dim palabra As String
        Dim componente As Control
        For Each componente In contenedor.Controls
            If TypeOf (componente) Is TextBox Then
                palabra = componente.Text
                If palabra <> "" Then
                    palabra = LCase(palabra)
                    Mid$(palabra, 1, 1) = UCase$(Mid$(palabra, 1, 1))
                    componente.Text = palabra
                End If
            End If
        Next
    End Sub

    'limpiar los txt en groupbox
    '-----------------------------
    Public Sub Limpiar(ByVal contenedor As Control)
        Dim componente As Control
        For Each componente In contenedor.Controls
            If TypeOf (componente) Is TextBox Then
                componente.Text = ""
            End If
            If TypeOf (componente) Is RichTextBox Then
                componente.Text = ""
            End If
            If TypeOf (componente) Is ComboBox Then
                componente.Text = "Seleccionar..."
            End If
        Next
    End Sub

    'Solo numeros
    '----------------------
    Public Sub Numeros(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Char.IsLetter(e.KeyChar) Then
            e.Handled = True
        Else
            If Char.IsControl(e.KeyChar) Then
                e.Handled = False
            Else
                If Char.IsSeparator(e.KeyChar) Then
                    e.Handled = True
                Else
                    If Char.IsDigit(e.KeyChar) Then
                        e.Handled = False
                    Else
                        e.Handled = True
                    End If
                End If
            End If
        End If
    End Sub

    'Solo letras en los Txt
    '--------------------------
    Public Sub Letras(ByVal txt As TextBox, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Char.IsLetter(e.KeyChar) Then
            e.Handled = False
        ElseIf Char.IsControl(e.KeyChar) Then
            e.Handled = False
        ElseIf Char.IsSeparator(e.KeyChar) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

    '**********************************************************************************************************************
    'Random IDentificador unico de llamada
    '**********************************************************************************************************************
    Public Sub IDentificadorUnico(ByVal etiqueta As Label)
        'Dim rnd1 As New Random()
        Dim myfecha As Date

        'Dim N As Integer = rnd1.Next(9999)
        myfecha = Now
        etiqueta.Text = myfecha.ToString("hhmmssMMddyyyy")
    End Sub

    '**********************************************************************************************************************
    'Control de combos no seleccionados, y vuenta a cero al salir del formulario o realizar el insert
    '**********************************************************************************************************************
    Public Function ControlCamposIncompletos(ByVal contenedor As Control)
        Dim componente As Control
        Dim ctrol, nombres As String
        nombres = ""
        ctrol = "no"
        For Each componente In contenedor.Controls
            If TypeOf (componente) Is ComboBox Then
                If componente.Text = "" Or componente.Text = "Seleccionar..." Then
                    ctrol = "si"
                End If
            End If
            If TypeOf (componente) Is TextBox Then
                If componente.Text = "" Then
                    ctrol = "si"
                End If
            End If
        Next
        If ctrol = "si" Then
            nombres = Chr(13) + " *-" + contenedor.Text + Chr(13)
        End If
        Return nombres
    End Function


End Module
